<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="信息描述">
        <el-popover
          placement="right"
          width="240"
          trigger="hover">
          <el-table :data="gridData">
            <el-table-column width="100" property="one" label="一级分类"></el-table-column>
            <el-table-column width="100" property="two" label="二级分类"></el-table-column>
          </el-table>
          <el-button slot="reference">excel模版示例</el-button>
        </el-popover>

        <el-tag>
          <i class="el-icon-download"/>
          <a :href="OSS_PATH + '/excel/subject.xlsx'">点击下载模版</a>
        </el-tag>

      </el-form-item>

      <el-form-item label="选择Excel">
        <el-upload
          ref="upload"
          :auto-upload="false"
          :on-success="fileUploadSuccess"
          :on-error="fileUploadError"
          :disabled="importBtnDisabled"
          :limit="1"
          :file-list="fileList"
          :before-upload="beforeUpload"
          :headers="headerObj"
          :action="BASE_URL+'/subject/addSubjectByExcel'"
          name="file"
          accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <el-button
            :loading="loading"
            style="margin-left: 10px;"
            size="small"
            type="success"
            @click="submitUpload">{{ fileUploadBtnText }}
          </el-button>
          <div slot="tip" class="el-upload__tip">只能上传 xlsx/xls文件，且不超过1M</div>
        </el-upload>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {getToken} from '@/utils/auth'

  export default {
    data() {
      return {
        BASE_URL: process.env.BASE_URL, // 接口API地址
        OSS_PATH: process.env.OSS_PATH, // 阿里云OSS地址
        fileUploadBtnText: '上传到服务器', // 按钮文字
        importBtnDisabled: false, // 按钮是否禁用,
        loading: false,
        fileList: [],
        gridData: [{
          one: '后端开发',
          two: 'java',
        }, {
          one: '后端开发',
          two: 'C++',
        }, {
          one: '前端',
          two: 'Vue',
        }, {
          one: '数据库',
          two: 'MySql',
        }],
        headerObj: {
          "token":  getToken() //添加token 否则无权限上传
        },
      }
    },
    methods: {
      submitUpload() {
        let files = this.$refs.upload.uploadFiles;
        if (files.length <= 0) return this.$message.error("请选择文件")
        this.fileUploadBtnText = '正在上传'
        this.importBtnDisabled = true
        this.loading = true
        this.$refs.upload.submit()
      },

      fileUploadSuccess(response) {
          this.fileUploadBtnText = '导入成功'
          this.loading = false
          this.$message.success("操作成功")
          this.$router.push({path: '/subject'})
      },
      // 文件上传前的校验 —— 常见的校验有：校验文件类型，文件大小
      beforeUpload(file) {
        // 允许上传的文件格式列表
        let acceptList = ["xlsx", "xls"]
        // 根据文件名获取文件的后缀名
        let fileType = file.name.split('.').pop().toLowerCase()
        // 判断文件格式是否符合要求
        if (acceptList.indexOf(fileType) === -1) {
          this.$message.error('只能上传 xlsx/xls 格式的文件 !');
          this.fileUploadBtnText = '上传到服务器'
          this.importBtnDisabled = false
          return false
        }
        // 判断文件大小是否符合要求
        if (file.size / 1024 / 1024 > 1) {
          this.$message.error('上传文件大小不能超过 1M !');
          this.fileUploadBtnText = '上传到服务器'
          this.importBtnDisabled = false
          return false
        }
      },

      fileUploadError(response) {
        this.fileUploadBtnText = '导入失败'
        this.loading = false
        this.$message({
          type: 'error',
          message: '导入失败'
        })
      }
    }

  }
</script>

<style scoped>

</style>
